package MineGod;

import java.io.IOException;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;


public class GUILabelHealthBar extends GUILabel{

	
	public static Texture empty;
	private static Texture full;
	private static Texture half;
		
	private static double[] hearts = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
	private static double heartWidth = 16;
	
	public GUILabelHealthBar(){
		x = 400;
		y = Chunk.CHUNK_PIXEL_HEIGHT + 24;
		width = 256;
		height = 64;
		active = true;
		hidden = false;
	}
	
	public static void loadTexture(){
		try {
			empty = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/EmptyHeart.png"));
			full = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/FullHeart.png"));
			half = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/HalfHeart.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public Texture getTexture() {
		return full;
	}
	
	public static void refreshHealthBar(){
		double hp = Math.max(MineGod.player.hp, 0);
		double numHearts = (hp/MineGod.player.maxHP)*20;
		for (int i = 0; i < hearts.length; i++){				
			if (i < Math.floor(numHearts)){
				hearts[i] = 1;
			} else {
				hearts[i] = 0;
			}
		}
		double leftover = Math.abs(numHearts - Math.floor(numHearts));
		if (leftover >= .5) {
			hearts[(int) Math.floor(numHearts)] = .5;
		}
	}
	
	public void render(double zIndex){
		if (!hidden && MineGod.player != null && !(MineGod.player.hp<0)){
			//GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
			//getTexture().bind();
			GL11.glColor3d(1, 1, 1);
			//GL11.glColor3d(.25, .25, .25);
			
			Texture tex;
			GL11.glDisable(GL11.GL_TEXTURE_2D);
			for(int i = 0; i < hearts.length; i++){
				if(hearts[i] == 1){
					tex = full;
				} else if (hearts[i] == .5){
					tex = half;
				} else {
					tex = empty;
				}
				tex.bind();
				GL11.glBegin(GL11.GL_QUADS);
					GL11.glNormal3d(0, 0, 1);
					GL11.glTexCoord2d(0.0, 0.0);
					GL11.glVertex3d(x + heartWidth*i, y, zIndex);
					GL11.glTexCoord2d(1.0, 0.0);
					GL11.glVertex3d(x + heartWidth*(i+1), y, zIndex);
					GL11.glTexCoord2d(1.0, 1.0);
					GL11.glVertex3d(x + heartWidth*(i+1), y + heartWidth, zIndex);
					GL11.glTexCoord2d(0.0, 1.0);
					GL11.glVertex3d(x + heartWidth*i, y + heartWidth, zIndex);
				GL11.glEnd();
				
			}
		}
	}
	
	
	
}
